//
//  NoticeDetailVc.m
//  BMProject
//
//  Created by 段大志 on 15/12/28.
//  Copyright (c) 2015 MengHuan. All rights reserved.
//

#import "NoticeDetailVc.h"
#import "DataModel.h"
#import "MHCommonViewHeader.h"
#import "UIViewController+common.h"
#import "UIColor+Extension.h"

@interface NoticeDetailVc () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *wrapView;
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UIWebView *webview;

@end

@implementation NoticeDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NAV_INIT(self, @"公告预览", @"back", @selector(backButtonClick:), nil, nil);
    _wrapView.layer.cornerRadius = COMMON_UIVIEW_Layer_CornerRadius;
    _wrapView.layer.masksToBounds = YES;
    _wrapView.layer.borderWidth = COMMON_UIVIEW_Layer_BorderWidth;
    _wrapView.layer.borderColor = [UIColor hexValue:0x333333].CGColor;
    _titleLable.text = _noticePageObject.title;

    // 测试webview访问百度
//    NSString *string = @"http://www.baidu.com";//测试
//    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:string]];
//    [_detailWebView loadRequest:request];

    NSString* htmlStr = @"";
    NSString *headStr = @"<head><style>img{max-width:250px !important;}</style></head>";
    htmlStr = [NSString stringWithFormat:@"%@%@", headStr, _noticePageObject.notice];
    //webview加载html
    [_webview loadHTMLString:htmlStr baseURL:[NSURL URLWithString:BASE_URL]];
    _webview.delegate = self;
    _webview.scalesPageToFit = NO;
}

#pragma mark webview delegate  WebView加载HTML图片大小自适应与文章自动换行(http://www.brighttj.com/ios/ios-webview-load-html-image-adaptive.html)
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_webview stringByEvaluatingJavaScriptFromString:
            @"var script = document.createElement('script');"
                    "script.type = 'text/javascript';"
                    "script.text = \"function ResizeImages() { "
                    "var myimg,oldwidth,oldheight;"
                    "var maxwidth=250;"// 图片宽度
                    "for(i=0;i  maxwidth){"
                    "myimg.width = maxwidth;"
                    "}"
                    "}"
                    "}\";"
                    "document.getElementsByTagName('head')[0].appendChild(script);"];
    [_webview stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
